<?php

use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

class AController extends ControllerBase {

    public function initialize() {
        $this->assets
                ->addCss('http://cdn.staticfile.org/twitter-bootstrap/3.1.1/css/bootstrap.min.css', false);
    }

    /**
     * Index action
     */
    public function indexAction() {
        $this->persistent->parameters = null;
    }

    /**
     * Searches for a
     */
    public function searchAction() {

        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "A", $_POST);
            $this->persistent->parameters = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = $this->persistent->parameters;
        if (!is_array($parameters)) {
            $parameters = array();
        }
        $parameters["order"] = "a";

        $a = A::find($parameters);
        if (count($a) == 0) {
            $this->flash->notice("The search did not find any a");

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "index"
            ));
        }

        $paginator = new Paginator(array(
            "data" => $a,
            "limit" => 1,
            "page" => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
    }

    /**
     * Displayes the creation form
     */
    public function newAction() {
        
    }

    /**
     * Edits a a
     *
     * @param string $a
     */
    public function editAction($a) {

        if (!$this->request->isPost()) {

            $a = A::findFirstBya($a);
            if (!$a) {
                $this->flash->error("a was not found");

                return $this->dispatcher->forward(array(
                            "controller" => "a",
                            "action" => "index"
                ));
            }

            $this->view->a = $a->a;

            $this->tag->setDefault("a", $a->a);
            $this->tag->setDefault("b", $a->b);
        }
    }

    /**
     * Creates a new a
     */
    public function createAction() {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "index"
            ));
        }

        $a = new A();

        $a->b = $this->request->getPost("b");


        if (!$a->save()) {
            foreach ($a->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "new"
            ));
        }

        $this->flash->success("a was created successfully");

        return $this->dispatcher->forward(array(
                    "controller" => "a",
                    "action" => "index"
        ));
    }

    /**
     * Saves a a edited
     *
     */
    public function saveAction() {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "index"
            ));
        }

        $a = $this->request->getPost("a");

        $a = A::findFirstBya($a);
        if (!$a) {
            $this->flash->error("a does not exist " . $a);

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "index"
            ));
        }

        $a->b = $this->request->getPost("b");


        if (!$a->save()) {

            foreach ($a->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "edit",
                        "params" => array($a->a)
            ));
        }

        $this->flash->success("a was updated successfully");

        return $this->dispatcher->forward(array(
                    "controller" => "a",
                    "action" => "index"
        ));
    }

    /**
     * Deletes a a
     *
     * @param string $a
     */
    public function deleteAction($a) {

        $a = A::findFirstBya($a);
        if (!$a) {
            $this->flash->error("a was not found");

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "index"
            ));
        }

        if (!$a->delete()) {

            foreach ($a->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                        "controller" => "a",
                        "action" => "search"
            ));
        }

        $this->flash->success("a was deleted successfully");

        return $this->dispatcher->forward(array(
                    "controller" => "a",
                    "action" => "index"
        ));
    }

}
